<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="办理人姓名" prop="operatorName">
        <el-input
          v-model="queryParams.operatorName"
          placeholder="请输入办理人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="办理人身份证" prop="operatorIdentification">
        <el-input
          v-model="queryParams.operatorIdentification"
          placeholder="请输入办理人身份证"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="办理人手机号码" prop="operatorPhone">
        <el-input
          v-model="queryParams.operatorPhone"
          placeholder="请输入办理人手机号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="办理人手机号码" prop="cellName">
        <el-input
          v-model="queryParams.cellName"
          placeholder="请输入办理人手机号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="逝者" prop="deathName">
        <el-input
          v-model="queryParams.deathName"
          placeholder="请输入逝者"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="时间">
        <el-date-picker
          v-model="daterangeTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="操作类型" prop="opType">
        <el-select v-model="queryParams.opType" placeholder="请选择操作类型" clearable size="small">
          <el-option
            v-for="dict in opTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            v-if="dict.dictValue > 1"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['fm:inOutOrder:add']"
        >迁入
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['fm:inOutOrder:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fm:inOutOrder:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['fm:inOutOrder:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inOutOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="办理人姓名" align="center" prop="operatorName"/>
      <el-table-column label="办理人身份证" align="center" prop="operatorIdentification"/>
      <el-table-column label="办理人手机号码" align="center" prop="operatorPhone"/>
      <el-table-column label="墓园ID" align="center" prop="cellName"/>
      <el-table-column label="逝者" align="center" prop="deathName"/>
      <el-table-column label="时间" align="center" prop="time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作类型" align="center" prop="opType">
        <template slot-scope="scope">
          <dict-tag :options="opTypeOptions" :value="scope.row.opType"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="statusOptions" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="订单编号" align="center" prop="cellOrderId"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.opType == 1 && scope.row.status==2"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="cellOut(scope.row)"
            v-hasPermi="['fm:inOutOrder:add']"
          >迁出
          </el-button>
          <!---
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['fm:inOutOrder:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['fm:inOutOrder:remove']"
          >删除</el-button>
          -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改出入土订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="办理人姓名" prop="operatorName">
          <el-input v-model="form.operatorName" placeholder="请输入办理人姓名"/>
        </el-form-item>
        <el-form-item label="办理人身份证" prop="operatorIdentification">
          <el-input v-model="form.operatorIdentification" placeholder="请输入办理人身份证"/>
        </el-form-item>
        <el-form-item label="办理人手机号码" prop="operatorPhone">
          <el-input v-model="form.operatorPhone" placeholder="请输入办理人手机号码"/>
        </el-form-item>

        <el-form-item label="墓园" prop="cellId" v-show="showAdd">
          <el-input v-model="form.cellId" placeholder="请输入墓园ID" v-show="false"/>
          <cellOrderSelect placeholder="请输入墓园ID" ref="cellOrderSelect" @orderSelect="orderSelect"/>
        </el-form-item>

        <el-form-item label="逝者" prop="deathId" v-show="showAdd">
          <dead-select v-model="form.deathId" placeholder="请输入逝者" ref="deadSelect"/>
        </el-form-item>
        <el-form-item label="操作类型" prop="opType" v-show="showAdd">
          <el-select v-model="form.opType" placeholder="请选择操作类型">
            <el-option
              v-for="dict in opTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单编号" prop="cellOrderId" v-show="false">
          <el-input v-model="form.cellOrderId" placeholder="请输入操作类型" disable/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInOutOrder,
  getInOutOrder,
  delInOutOrder,
  addInOutOrder,
  updateInOutOrder,
  exportInOutOrder, addOutOrder
} from "@/api/fm/inOutOrder";
import deadSelect from "@/views/fm/dead/deadSelect";
import cellOrderSelect from "@/views/fm/cellOrder/cellOrderSelect";

export default {
  name: "InOutOrder",
  components: {deadSelect, cellOrderSelect},
  data() {
    return {
      showAdd: true,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出入土订单表格数据
      inOutOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 时间时间范围
      daterangeTime: [],
      // 操作类型字典
      opTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        operatorName: null,
        operatorIdentification: null,
        operatorPhone: null,
        cellName: null,
        deathName: null,
        time: null,
        opType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        operatorName: [
          {required: true, message: "办理人姓名不能为空", trigger: "blur"}
        ],
        operatorIdentification: [
          {required: true, message: "办理人身份证不能为空", trigger: "blur"}
        ],
        operatorPhone: [
          {required: true, message: "办理人手机号码不能为空", trigger: "blur"}
        ],
        cellId: [
          {required: true, message: "墓园ID不能为空", trigger: "onSubmit"}
        ],
        deathId: [
          {required: true, message: "逝者ID不能为空", trigger: "onSubmit"}
        ],
        opType: [
      {required: true, message: "请选择操作类型", trigger: "onSubmit"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("fm_cell_op").then(response => {
      this.opTypeOptions = response.data;
    });
    this.getDicts("fm_sell_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询出入土订单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeTime && '' != this.daterangeTime) {
        this.queryParams.params["beginTime"] = this.daterangeTime[0];
        this.queryParams.params["endTime"] = this.daterangeTime[1];
      }
      listInOutOrder(this.queryParams).then(response => {
        this.inOutOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        loginId: null,
        operatorName: null,
        operatorIdentification: null,
        operatorPhone: null,
        cellId: null,
        cellName: null,
        deathId: null,
        deathName: null,
        time: null,
        opType: null,
        cellOrderId: null,
        showAdd: true,
        status: 2
      };

      this.resetForm("form");
      if (this.$refs['deadSelect']) {
        this.$refs['deadSelect'].reset();
      }
      if (this.$refs['cellOrderSelect']) {
        this.$refs['cellOrderSelect'].reset();
      }
    },

    orderSelect(data) {
      this.form.cellId = data[0];
      this.form.cellOrderId = data[1];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.showAdd = true;
      this.title = "办理入土业务";
    },
    /** 出土订单 */
    cellOut(row) {
      this.reset();
      this.open = true;
      this.title = "办理出土业务";
      this.form.id = row.id;
      this.form.cellId = row.cellId;
      this.form.deathId = row.deathId;
      this.form.cellOrderId = row.cellOrderId;
      this.form.opType = 2;
      this.form.status = 4;
      this.showAdd = false;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInOutOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改出入土订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.opType == 2) {
            addOutOrder(this.form).then(response => {
              this.msgSuccess("迁出完成");
              this.open = false;
              this.getList();
            });
          } else if (this.form.id != null) {
            updateInOutOrder(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInOutOrder(this.form).then(response => {
              this.msgSuccess("迁入完成");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除出入土订单编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delInOutOrder(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有出入土订单数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportInOutOrder(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    }
  }
};
</script>
